package com.mp.controller;

import com.mp.ws.registro.Item;
import com.mp.ws.registro.NomeclaturaLocal;
import com.mp.ws.registro.RegistroWS_Service;
import com.mp.ws.registro.TipoUnidade;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;


@WebServlet(name = "Registro", urlPatterns = {"/Registro"})
public class Registro extends HttpServlet 
{
    @WebServiceRef
            (
             wsdlLocation = "WEB-INF/wsdl/localhost_8080/" +
            "MemoryPriceServerWS/RegistroWS.wsdl"
            )
    private RegistroWS_Service service;

   
    protected void processRequest(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {
      
    }

   
    @Override
    protected void doGet(HttpServletRequest request, 
    HttpServletResponse response)
            throws ServletException, IOException 
    {
        processRequest(request, response);
        
        String sessao = (String) request.getAttribute("idSessaoUsuario");
        List<NomeclaturaLocal> listaLocal = getListaNomeclatura(sessao);
        List<Item> listaItem = getListaItem(sessao);
        
        TipoUnidade tpUnidade = null;
        
        if(listaItem.size() > 0)
        {
           tpUnidade  = getTipoUnidade
                   (
                     sessao,
                     String.valueOf( ((Item)listaItem.get(0)).getIdItem() )
                   );
        }
              
                     
        String comboLocal = "<select id='idLocal' name='idLocal'>";
        
        for(NomeclaturaLocal local : listaLocal)
        {
           comboLocal = comboLocal + "<option value='" + 
                   local.getIdNomeclaturaLocal() + "'>" + 
                   local.getLabelNomeclaturaLocal() + 
                   "</option>";
        }
        
        comboLocal = comboLocal + "</select>";
        
        
        String comboItem = "<select id='idItem' name='idItem'>";
        
        for(Item item : listaItem)
        {
           comboItem  = comboItem  + "<option value='" + 
                   item.getIdItem() + "'>" 
                   + item.getDescItem() + "</option>";
        }
        
        comboItem  = comboItem + "</select>";
        
        
        request.setAttribute("idSessaoUsuario",sessao);
        request.setAttribute("comboLocal",comboLocal);
        request.setAttribute("comboItem",comboItem);
        request.setAttribute("tipoUnidade",tpUnidade.getDescTipoUnidade());
        
        RequestDispatcher view = request.getRequestDispatcher("/registro.jsp");
        view.forward(request, response);
        
    }

   
    @Override
    protected void doPost(HttpServletRequest request, 
    HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    private java.util.List<com.mp.ws.registro.Item> 
            getListaItem(java.lang.String idSessaoUsuario) 
    {
        com.mp.ws.registro.RegistroWS port = service.getRegistroWSPort();
        return port.getListaItem(idSessaoUsuario);
    }

    private java.util.List<com.mp.ws.registro.NomeclaturaLocal> 
            getListaNomeclatura(java.lang.String idSessaoUsuario) 
    {
        com.mp.ws.registro.RegistroWS port = service.getRegistroWSPort();
        return port.getListaNomeclatura(idSessaoUsuario);
    }

    private TipoUnidade getTipoUnidade(java.lang.String idSessaoUsuario, java.lang.String idItem) {
        com.mp.ws.registro.RegistroWS port = service.getRegistroWSPort();
        return port.getTipoUnidade(idSessaoUsuario, idItem);
    }
    
    
}
